<template>
  <el-dialog width="60%" :title="!dataForm.id ? '角色新增' : '角色编辑'" :visible="childVisible" :before-close="handleCancel" :close-on-click-modal='false'>
    <el-form :rules="rules" ref="dataForm" :model="dataForm" label-position="right" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-tabs type="border-card">
            <el-tab-pane label="角色基本信息">
              <div style="height:300px; ">
                <el-form-item label="角色编号" prop="code">
                  <el-input placeholder="请填写角色编号" v-model.trim="dataForm.code" v-bind:disabled="disabledInput"></el-input>
                </el-form-item>
                <el-form-item label="角色名称" prop="name">
                  <el-input placeholder="请填写角色名称" v-model.trim="dataForm.name"></el-input>
                </el-form-item>
              </div>
            </el-tab-pane>
          </el-tabs>
        </el-col>
        <el-col :span="12">
          <el-tabs type="border-card">
            <el-tab-pane label="授权">
              <div style="height:300px; overflow-y:scroll; ">
                <el-tree :data="menuList" :props="menuListTreeProps" node-key="id" ref="menuListTree" :default-expand-all="true" show-checkbox/>
              </div>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="childVisible = false">取消</el-button>
      <el-button type="primary" v-bind:loading="buttonLoading" @click="dataFormSubmit">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { validPhone, validateEmail } from '@/utils/validate'
import { insertRole, updateRole, getRoleDetail } from '@/api/auth/role'
import { getMenuList } from '@/api/auth/resource'
import { treeDataTranslate } from '@/utils'

export default {
  props: ['myVisible'],
  data() {
    return {
      buttonLoading: false,
      disabledInput: false,
      childVisible:this.myVisible,
      tempKey: "-1",
      menuList: [],
      menuListTreeProps: {
        label: 'name',
        children: 'children'
      },
      statusMap: {
        create: '新增',
        update: '编辑'
      },
      dataForm: {
        id: 0,
        code: '',
        name: '',
        menuList: []
      },
      props: {
        id: 'id',
        label: 'name',
        children: 'children'
      },
      rules: {
        code: [{ required: true, message: '角色编号不能为空' },
        { max: 50, message: '最多输入 50 个字符', trigger: 'blur' }],
        name: [
          { required: true, trigger: 'blur', message: '角色名称不能为空' }
          ,
          { max: 50, message: '最多输入 50 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    childVisible(val) {
      // 修改父元件的值
      console.log("子组件值发生改变："+val)
      this.$emit('update:myVisible', val)
    }
  },
  methods: {
    getResourceTreeList(id) {
      this.dataForm.id = id;
      getMenuList().then((response) => {
        this.menuList = treeDataTranslate(response.data, 'id')
      }).then(() => {
        this.childVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
         // 新增
        if (!this.dataForm.id) {
          this.disabledInput = false;
          this.buttonLoading = false;
        } else {
          // 修改
          this.disabledInput = true;
          getRoleDetail(this.dataForm.id).then((response) => {
            this.dataForm.code = response.data.code
            this.dataForm.name = response.data.name
            this.$refs.menuListTree.setCheckedKeys(response.data.menuList)
          })
        }
      })
    },
    dataFormSubmit() {
      this.buttonLoading = true;
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.id) {
            this.dataForm.menuList = [].concat(this.$refs.menuListTree.getCheckedKeys(), this.$refs.menuListTree.getHalfCheckedKeys());
            insertRole(this.dataForm).then((response) => {
              this.$notify({
                message: '新增成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.childVisible = false;
                  this.buttonLoading = false;
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            this.dataForm.menuList = [].concat(this.$refs.menuListTree.getCheckedKeys(), [this.tempKey], this.$refs.menuListTree.getHalfCheckedKeys());
            updateRole(this.dataForm).then((response) => {
              this.$notify({
                message: '修改成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.childVisible = false;
                  this.buttonLoading = false;
                  this.$emit('refreshDataList');
                }
              })
            })
          }
        } else {
          this.buttonLoading = false;
        }
      })
    },
    handleCancel() {
      // 点击叉叉
      this.childVisible = false
    }
  }
}
</script>
